package com.dyylearn.http.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("LoginServlet doPost()被调用");

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>登录成功~</h1>");

        writer.flush();
        writer.close();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        //服务端输出
        System.out.println("LoginServlet doGet()被调用");
        //输出一句话，返回给浏览器
        //1.通 response 获取流 PrintWriter，可以给浏览器回复数据
        //2.为了让浏览器显示中文，需要告诉浏览器，我们的编码是utf-8
        //  1）给会送数据设置编码
        //  2）text/html这个是MIME即告诉浏览器返回的数据是text类型下的
        //   html格式数据【MIME类型大类型/小类型】
        //  3）charset=utf-8数据编码
        //细节：设置编码格式要在response.getWriter()之前
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print("<h1>登录成功~</h1>");

        //为了确保数据返回，可以把fLush（）和close（）
        //fLush（）方法表示将缓存的数据进行刷新
        writer.flush();
        //close（）表示关闭流，及时释放资源
        writer.close();
    }
}
